package com.fr.report.web;

import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.XMLable;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.web.core.MailAuthenticator;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailManager
  implements XMLable
{
  private String mailHost = null;
  private String user = null;
  private String password = null;
  private String fromEmailAddress = null;
  private transient Properties mailProperties;

  public String getMailHost()
  {
    return this.mailHost;
  }

  public void setMailHost(String paramString)
  {
    this.mailHost = paramString;
  }

  public String getUser()
  {
    return this.user;
  }

  public void setUser(String paramString)
  {
    this.user = paramString;
  }

  public String getPassword()
  {
    return this.password;
  }

  public void setPassword(String paramString)
  {
    this.password = paramString;
  }

  public String getFromEmailAddress()
  {
    return this.fromEmailAddress;
  }

  public void setFromEmailAddress(String paramString)
  {
    this.fromEmailAddress = paramString;
  }

  public void sendTestMail(String paramString)
    throws MessagingException
  {
    send(paramString, "Email Test", Inter.getLocText("ReportServerP-Congratulation_Email_Test_Success"));
  }

  public void send(String paramString1, String paramString2, String paramString3)
    throws MessagingException
  {
    send(paramString1, null, null, paramString2, paramString3, null);
  }

  public void send(String paramString1, String paramString2, String paramString3, String paramString4)
    throws MessagingException
  {
    send(paramString1, null, null, null, paramString2, paramString3, null, paramString4);
  }

  public void send(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, Object paramObject)
    throws MessagingException
  {
    send(paramString1, paramString2, paramString3, null, paramString4, paramString5, paramObject, null);
  }

  public void send(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6, Object paramObject)
    throws MessagingException
  {
    send(paramString1, paramString2, paramString3, paramString4, paramString5, paramString6, paramObject, null);
  }

  public void send(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6, Object paramObject, String paramString7)
    throws MessagingException
  {
    int i;
    Properties localProperties = asMailProperties();
    Session localSession = Session.getInstance(localProperties, new MailAuthenticator(localProperties.getProperty("mail.user"), localProperties.getProperty("mail.password")));
    MimeMessage localMimeMessage = new MimeMessage(localSession);
    if (StringUtils.isNotBlank(paramString1))
      localMimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(paramString1));
    if (StringUtils.isNotBlank(paramString2))
      localMimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(paramString2));
    if (StringUtils.isNotBlank(paramString3))
      localMimeMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(paramString3));
    if (StringUtils.isBlank(paramString4))
    {
      paramString4 = this.fromEmailAddress;
      if (StringUtils.isBlank(paramString4))
        paramString4 = Inter.getLocText("HTML-Report_Mail_Server");
    }
    localMimeMessage.setFrom(new InternetAddress(paramString4));
    MimeBodyPart localMimeBodyPart1 = new MimeBodyPart();
    if (paramString7 == null)
      localMimeBodyPart1.setText(paramString6);
    else
      localMimeBodyPart1.setContent(paramString6, paramString7);
    MimeMultipart localMimeMultipart = new MimeMultipart();
    localMimeMultipart.addBodyPart(localMimeBodyPart1);
    localMimeMessage.setSubject(paramString5);
    localMimeMessage.setContent(localMimeMultipart);
    localMimeMessage.setSentDate(new Date());
    if (paramObject instanceof File)
    {
      localObject1 = (File)paramObject;
      if ((localObject1 != null) && (((File)localObject1).exists()))
      {
        MimeBodyPart localMimeBodyPart2 = new MimeBodyPart();
        localObject2 = new FileDataSource((File)localObject1);
        localMimeBodyPart2.setDataHandler(new DataHandler((DataSource)localObject2));
        localMimeBodyPart2.setFileName(((File)localObject1).getName());
        localMimeMultipart.addBodyPart(localMimeBodyPart2);
      }
    }
    else
    {
      if (paramObject instanceof File[])
      {
        localObject1 = (File[])paramObject;
        i = 0;
        while (true)
        {
          if (i >= localObject1.length)
            break label477;
          localObject2 = new MimeBodyPart();
          localObject3 = new FileDataSource(localObject1[i]);
          ((MimeBodyPart)localObject2).setDataHandler(new DataHandler((DataSource)localObject3));
          ((MimeBodyPart)localObject2).setFileName(localObject1[i].getName());
          localMimeMultipart.addBodyPart((BodyPart)localObject2);
          ++i;
        }
      }
      if (paramObject instanceof String[])
      {
        localObject1 = (String[])paramObject;
        for (i = 0; i < localObject1.length; ++i)
        {
          localObject2 = new MimeBodyPart();
          localObject3 = new FileDataSource(localObject1[i]);
          ((MimeBodyPart)localObject2).setDataHandler(new DataHandler((DataSource)localObject3));
          ((MimeBodyPart)localObject2).setFileName(localObject1[i]);
          localMimeMultipart.addBodyPart((BodyPart)localObject2);
        }
      }
    }
    label477: localMimeMessage.saveChanges();
    Object localObject1 = localSession.getTransport("smtp");
    ((Transport)localObject1).connect();
    Address[] arrayOfAddress1 = localMimeMessage.getRecipients(Message.RecipientType.TO);
    if (arrayOfAddress1 == null)
      arrayOfAddress1 = new Address[0];
    Object localObject2 = localMimeMessage.getRecipients(Message.RecipientType.CC);
    if (localObject2 == null)
      localObject2 = new Address[0];
    Object localObject3 = localMimeMessage.getRecipients(Message.RecipientType.BCC);
    if (localObject3 == null)
      localObject3 = new Address[0];
    Address[] arrayOfAddress2 = new Address[arrayOfAddress1.length + localObject2.length + localObject3.length];
    System.arraycopy(arrayOfAddress1, 0, arrayOfAddress2, 0, arrayOfAddress1.length);
    System.arraycopy(localObject2, 0, arrayOfAddress2, arrayOfAddress1.length, localObject2.length);
    System.arraycopy(localObject3, 0, arrayOfAddress2, arrayOfAddress1.length + localObject2.length, localObject3.length);
    ((Transport)localObject1).sendMessage(localMimeMessage, arrayOfAddress2);
    ((Transport)localObject1).close();
  }

  private Properties asMailProperties()
  {
    if (this.mailProperties == null)
    {
      this.mailProperties = new Properties();
      this.mailProperties.put("mail.smtp.host", this.mailHost);
      this.mailProperties.put("mail.transport.protocol", "smtp");
      this.mailProperties.put("mail.smtp.starttls.enable", "true");
      this.mailProperties.put("mail.smtp.auth", "true");
      this.mailProperties.put("mail.user", this.user);
      this.mailProperties.put("mail.password", this.password);
    }
    return this.mailProperties;
  }

  public JSONObject createJSON()
    throws JSONException
  {
    JSONObject localJSONObject = new JSONObject();
    localJSONObject.put("serverName", this.mailHost);
    localJSONObject.put("fromAddress", this.fromEmailAddress);
    localJSONObject.put("userName", this.user);
    localJSONObject.put("passWord", this.password);
    return localJSONObject;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws JSONException
  {
    if (StringUtils.isNotEmpty(paramJSONObject.getString("serverName")))
      setMailHost(paramJSONObject.getString("serverName"));
    if (StringUtils.isNotEmpty(paramJSONObject.getString("fromAddress")))
      setFromEmailAddress(paramJSONObject.getString("fromAddress"));
    if (StringUtils.isNotEmpty(paramJSONObject.getString("userName")))
      setUser(paramJSONObject.getString("userName"));
    if (StringUtils.isNotEmpty(paramJSONObject.getString("passWord")))
      setPassword(paramJSONObject.getString("passWord"));
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("EmailManager").attr("mailHost", getMailHost()).attr("password", ReportXMLUtils.passwordString(getPassword())).attr("user", getUser()).attr("fromEmailAddress", getFromEmailAddress()).end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
    {
      String str;
      if ((str = paramXMLableReader.getAttr("mailHost")) != null)
        setMailHost(str);
      if ((str = paramXMLableReader.getAttr("fromEmailAddress")) != null)
        setFromEmailAddress(str);
      if ((str = paramXMLableReader.getAttr("password")) != null)
        setPassword(ReportXMLUtils.unPasswordString(str));
      if ((str = paramXMLableReader.getAttr("user")) != null)
        setUser(str);
    }
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }
}